import { bg_image } from "./indoorConfig.js";
import {getMerchantInfo} from "../api/home/index.js"
import {balanceRecordList} from "../api/merchant/index.js"
export default {
	data() {
		return {
			bg_image,
			list: [],
			status: 'more', //more/loading/noMore
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中',
				contentnomore: '没有更多'
			},
			query: {
				merchantId: uni.getStorageSync('merchantId'),
				page: 1,
				pageSize: 10
			},
			date: this.getCurrentMonth(),
		}
	},
	onReady() {
		this.getList()
	},
	filters: {
		filterType(val) {
			if (val === 1) {
				return '商家充值收入'
			}
			if (val === 2) {
				return '后台充值收入'
			}
			if (val === 3) {
				return '优惠券二维码支出'
			}
			if (val === 4) {
				return '发送优惠券支出'
			}
			if (val === 5) {
				return '二维码到期退款'
			}
			if (val === 6) {
				return '优惠券到期未使用退款'
			}
			if (val === 7) {
				return '商家全免券部分退款'
			}

		}
	},
	computed: {
		startDate() {
			return this.getDate('start');
		},
		endDate() {
			return this.getDate('end');
		}
	},
	methods: {
		async fetchSomething(query){
			this.query.page = 1
			this.getList()
		},
		getDayNum() {
			let month = this.date.split('-')[1]
			if(!month) month = this.date.split('/')[1]
			if (month.indexOf("0") === 0) {
				month = Number(month.slice(1))
			}else{
				month=Number(month)
			}
			console.log("month=", month)
			let day = 31;
			const date = new Date()
			let year = date.getFullYear()
			switch (month) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					day = 31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					day = 30;
					break;
				case 2:
					if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
						day = 29;
					} else {
						day = 28;
					}
					break;
				default:
					day = 31
			}
			return day;
		},
		async getDetail() {
			let res = await getMerchantInfo()
			this.data = res.data || {}
		},
		getCurrentMonth() {
			const date = new Date()
			let year = date.getFullYear()
			let month = date.getMonth() + 1
			month = month > 9 ? month : '0' + month
			return `${year}-${month}`
		},
		getDate(type) {
			const date = new Date();
			let year = date.getFullYear();
			let month = date.getMonth() + 1;
			let day = date.getDate();

			if (type === 'start') {
				year = year - 60;
			} else if (type === 'end') {
				year = year + 2;
			}
			month = month > 9 ? month : '0' + month;
			day = day > 9 ? day : '0' + day;
			// return `${year}-${month}-${day}`;
			return `${year}-${month}-${day}`;
		},
		bindDateChange(e) {
			console.log('e',e)
			let value = e.detail.value
			this.date = value.replace('/','-')
			this.list=[]
			this.getList()
		},
		async getList() {
			let day = this.getDayNum()
			this.query.startTime = `${this.date}-01 00:00:00`
			this.query.endTime = `${this.date}-${day>9?day:'0'+day} 23:59:59`
			console.log('this.query',this.query)
			let res = await balanceRecordList(this.query)
			if(!res) return this.status = 'more'
			if(this.query.page == 1) this.list = []
			this.query.page += 1
			let data = res.data || {}
			let list = data.list || []
			this.list = [...this.list, ...list]
			if (list.length === 0) {
				this.status = 'noMore'
				return false;
			}
			this.status = 'more'
		},
		clickLoadMore(e) {
			this.status = 'loading'
			this.getList()
		}
	},
}